package com.controller.sys;


import cn.hutool.core.util.IdUtil;
import com.common.base.BaseController;
import com.entity.sys.SysMenu;
import com.service.sys.SysMenuService;
import fuhua.com.anno.CheckPermission;
import fuhua.com.anno.Log;
import fuhua.com.util.ResultUtil;
import fuhua.com.util.TreeUtil;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单权限
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController<SysMenuService,SysMenu> {

    /**
     * 菜单列表
     */
    @GetMapping("/list")
    @CheckPermission(perm = "system:menu:view")
    public ResultUtil list(@RequestParam Map<String,String> map) {
        List<SysMenu> menus = service.selectMenuList(map);
        return ResultUtil.success(menus);
    }

    /**
     * 路由列表
     */
    @GetMapping("/getRouters")
    public ResultUtil getRouters() {
        Map<String,String> query = new HashMap<>();
        query.put("id",getLoginUser().getRoleId());
        List<SysMenu> menus = service.selectMenuTreeByRoleId(query);
        return ResultUtil.success(service.buildMenus(menus));
    }

    /**
     * 根据id获取信息
     */
    @GetMapping("/getById/{id}")
    public ResultUtil getById(@PathVariable("id")String id) {
        return ResultUtil.success(service.getById(id));
    }

    /**
     * 菜单下拉树列表
     */
    @GetMapping("/treeMenuList")
    public ResultUtil treeMenuList(@RequestParam Map<String,String> map) {
        List<SysMenu> menus = service.selectMenuList(map);
        return ResultUtil.success(TreeUtil.buildTreeIsRoot(menus,"ROOT"));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTree/{roleId}")
    public ResultUtil roleMenuTree(@PathVariable("roleId") String roleId) {
        Map<String,Object> map = new HashMap<>();
        Map<String,String> queryVo = new HashMap<>();
        List<SysMenu> menus = service.selectMenuList(queryVo);
        List<SysMenu> treeVos = TreeUtil.buildTreeIsRoot(menus,"ROOT");
        map.put("menus", treeVos);
        queryVo.put("id",roleId);
        map.put("checkedKeys", service.selectIdListByRoleId(queryVo));
        return ResultUtil.success(map);
    }

    /**
     * 新增
     */
    @Log(title = "新增菜单")
    @PostMapping("/insert")
    @CheckPermission(perm = "system:menu:add")
    public ResultUtil insert(@RequestBody SysMenu menu){
        if (service.checkMenuNameUnique(menu)) {
            return ResultUtil.error("菜单名称已存在");
        }
        menu.setId(IdUtil.getSnowflakeNextIdStr());
        service.save(menu);
        return ResultUtil.success();
    }

    /**
     * 修改
     */
    @Log(title = "修改菜单")
    @PostMapping("/update")
    @CheckPermission(perm = "system:menu:update")
    public ResultUtil update(@RequestBody SysMenu menu){
        SysMenu m = service.getById(menu.getId());
        if (m != null && !m.getName().equals(menu.getName())){
            if (service.checkMenuNameUnique(menu)) {
                return ResultUtil.error("菜单名称已存在");
            }
            if (menu.getId().equals(menu.getParentId())) {
                return ResultUtil.error("上级菜单不能选择自己");
            }
        }
        service.updateById(menu);
        if (!m.getPerms().equals(menu.getPerms())){ // 更改了权限标识，在redis缓存中也需要更改
            service.updatePerm(m.getPerms(),menu.getPerms());
        }
        return ResultUtil.success();
    }

    /**
     * 删除菜单
     */
    @Log(title = "删除菜单")
    @DeleteMapping("/delete/{id}")
    @CheckPermission(perm = "system:menu:delete")
    public ResultUtil delete(@PathVariable("id") String id){
        if (service.hasChildByMenuId(id)) {
            return ResultUtil.error("存在子菜单,不允许删除");
        }
        if (service.checkMenuExistRole(id)) {
            return ResultUtil.error("菜单已分配,不允许删除");
        }
        return ResultUtil.success(service.removeById(id));
    }
}
